<template>
    <div class="app-container">
      <el-card v-if="coupon.type != 3">
        <div slot="header" class="clearfix">
          <span>{{$t('优惠券使用统计')}}</span>
        </div>
        <div class="table-layout">
          <el-row :gutter="20">
            <el-col :span="4" class="table-cell-title">{{$t('已领取数量')}}</el-col>
            <el-col :span="4" class="table-cell-title">{{$t('已使用数量')}}</el-col>
            <el-col :span="4" class="table-cell-title">{{$t('使用优惠券订单总额')}}</el-col>
            <el-col :span="4" class="table-cell-title">{{$t('总优惠金额')}}</el-col>
            <el-col :span="4" class="table-cell-title">{{$t('总订单价格')}}</el-col>
          </el-row>
          <el-row>
            <el-col :span="4" class="table-cell">{{ statistics.pullNumber }}</el-col>
            <el-col :span="4" class="table-cell">{{ statistics.usedNumber }}</el-col>
            <el-col :span="4" class="table-cell">${{ statistics.orderTotalPrice }}</el-col>
            <el-col :span="4" class="table-cell">$ {{ statistics.orderTotalDiscount  }}</el-col>
            <el-col :span="4" class="table-cell">$ {{ statistics.orderTotalActual }}</el-col>
          </el-row>
        </div>
      </el-card>

      <el-card>
        <!-- 查询操作 -->
        <div v-if="coupon.type == 3" class="filter-container">
          <el-input v-model="listQuery.userId" clearable class="filter-item" style="width: 200px;" :placeholder="$t('Please_enter_member_ID')"/>
          <el-input v-model="listQuery.id" clearable class="filter-item" style="width: 200px;" :placeholder="$t('请输入优惠券码')"/>
          <el-select v-model="listQuery.status" clearable style="width: 200px" class="filter-item" :placeholder="$t('请选择使用状态')">
            <el-option v-for="type in useStatusOptions" :key="type.value" :label="type.label" :value="type.value"/>
          </el-select>

          <el-date-picker v-model="listQuery.addTime" class="filter-item clear-icon"
                          :editable="false" :clearable="true" :placeholder="$t('领券时间')"  format="yyyy 年 MM 月 dd 日"
                          value-format="yyyy-MM-dd"></el-date-picker>

          <el-date-picker v-model="listQuery.usedTime" class="filter-item clear-icon"  :editable="false"
                          :clearable="true" :placeholder="$t('使用时间')"  format="yyyy 年 MM 月 dd 日"
                          value-format="yyyy-MM-dd"></el-date-picker>

          <el-button v-permission="['GET /admin/coupon/listuser']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{$t('Search')}}</el-button>
        </div>

        <!-- 查询结果 -->
        <el-table v-if="coupon.type == 3" v-loading="listLoading" :data="list" :element-loading-text="$t('Searching')" border fit highlight-current-row>
          <el-table-column type="index" :index="getIndex" width="50"></el-table-column>

          <el-table-column align="center" :label="$t('用户优惠券ID')" prop="id" sortable/>

          <el-table-column align="center" :label="$t('Member_ID')" prop="userId"/>

          <el-table-column align="center" :label="$t('领取时间')" prop="addTime"/>

          <el-table-column align="center" :label="$t('使用状态')" prop="status">
            <template slot-scope="scope">{{ formatUseStatus(scope.row.status)  }}</template>
          </el-table-column>

          <el-table-column align="center" :label="$t('订单编号')" prop="orderSn"/>

          <el-table-column align="center" :label="$t('使用时间')" prop="usedTime"/>

        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

      </el-card>

    </div>
</template>

<script>
import { readCoupon, listCouponUser, listCouponGoods, couponStatistics, listCouponCategory } from '@/api/coupon'
import Pagination from '@/components/Pagination'

export default {
  name: 'extenCouponStatistic',
  components: {Pagination},
  data(){
    const defaultUseStatusOptions = [
      {
        label: this.$t('未使用'),
        value: 0
      },
      {
        label: this.$t('已使用'),
        value: 1
      },
      {
        label: this.$t('Expired'),
        value: 2
      },
      {
        label: this.$t('Merchandise_removed'),
        value: 3
      }
    ]

    return{
      listLoading: false,
      useStatusOptions: Object.assign({}, defaultUseStatusOptions),
      statistics:{
        pullNumber: undefined,
        usedNumber: undefined,
        orderTotalPrice: undefined,
        orderTotalDiscount: undefined,
        orderTotalActual: undefined,
      },
      coupon: {},
      listGoodsQuery: {
        page: 1,
        limit: 20,
        id: 0,
        sort: 'add_time',
        order: 'desc'
      },
      list: [],
      listQuery: {
        page: 1,
        limit: 20,
        couponId: undefined,
        userId: undefined,
        status: undefined,
        sort: 'add_time',
        order: 'desc',
        shopId: undefined,
        addTime: undefined,
        usedTime: undefined,
        id: undefined,
      },
    }
  },
  created() {
    this.init()
    if (this.$route.query.id == null) {
      return
    }
    this.listGoodsQuery.id = this.$route.query.id
    couponStatistics({id: this.listGoodsQuery.id}).then(response=>{
      this.statistics = response.data.data
    })
  },
  methods: {
    init: function() {
      if (this.$route.query.id == null) {
        return
      }
      readCoupon(this.$route.query.id).then(response => {
        this.coupon = response.data.data
      })
      this.listQuery.couponId = this.$route.query.id
      this.getList()
    },
    getList() {
      this.listLoading = true
      listCouponUser(this.listQuery)
        .then(response => {
          this.list = response.data.data.content
          this.total = response.data.data.totalElements
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getIndex(index) {
      return (index + 1) + (this.listQuery.page - 1) * this.listQuery.limit;
    },
    formatUseStatus(status) {
      if (status === 0) {
        return this.$t('未使用')
      } else if (status === 1) {
        return this.$t('已使用')
      } else if (status === 2) {
        return this.$t('Expired')
      } else {
        return this.$t('Merchandise_removed')
      }
    },
  }
}
</script>

<style scoped>
.filter-container {
  margin-top: 20px;
}

.table-layout {
  margin-top: 20px;
  border-left: 1px solid #DCDFE6;
  border-top: 1px solid #DCDFE6;
}
.table-cell {
  height: 60px;
  line-height: 40px;
  border-right: 1px solid #DCDFE6;
  border-bottom: 1px solid #DCDFE6;
  padding: 10px;
  font-size: 14px;
  color: #606266;
  text-align: center;
  overflow: hidden;
}
.table-cell-title {
  border-right: 1px solid #DCDFE6;
  border-bottom: 1px solid #DCDFE6;
  padding: 10px;
  background: #F2F6FC;
  text-align: center;
  font-size: 14px;
  color: #303133;
}
</style>
